//
//  ZGuessController.m
//  NewProject
//
//  Created by 陈洪鑫 on 2018/4/13.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZWalletController.h"
//#import "ZBaseTableView.h"
#import "ZMyHeaderCell.h"
#import "ZWalletDetailController.h"
#import "ZWalletListCell.h"
#import "ZCreateWalletController.h"
#import "ZImportPrivateController.h"
#import "ZWalletInfo.h"
@interface ZWalletController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
//@property (nonatomic,strong) ZWalletAccount *wallet;
@property (nonatomic,strong) NSMutableArray<ZWalletAccountInfo*> *wallets;
@property (nonatomic,assign) NSInteger page;
@end

@implementation ZWalletController
- (NSMutableArray *)wallets
{
    if (!_wallets) {
        _wallets = [NSMutableArray array];
    }return _wallets;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat navHeight = iPhoneX ? 44 : 20;
        CGFloat bottom = iPhoneX ? 34 + 49 : 49;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, -navHeight, SCREEN_W, SCREEN_H - bottom + navHeight) collectionViewLayout:[UICollectionViewFlowLayout new]];
        _collectionView.backgroundColor = [UIColor colorWithHexString:@"#eaeaea"];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"ZMyHeaderCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ZMyHeaderCell"];
        
         [_collectionView registerNib:[UINib nibWithNibName:@"ZWalletListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ZWalletListCell"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }return _collectionView;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    [self.collectionView reloadData];
//    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.collectionView.mj_header endRefreshing];
     self.navigationController.navigationBarHidden = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pais_change) name:@"z_wallet_pais_change" object:nil];
  
//    self.wallet = [ZWalletAccount wallet];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(qureyAccountInfo)];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView.mj_header beginRefreshing];
    [self.view addSubview:self.collectionView];
}
- (void)qureyAccountInfo{
    self.page = 0;
    [self.wallets removeAllObjects];
    ZWWeakSelf;
    [ZDBManager queryWallets:self.page completion:^(NSArray *wallets) {
        [weakSelf.collectionView.mj_header endRefreshing];
        NSArray *items = [ZWalletAccountInfo mj_objectArrayWithKeyValuesArray:wallets];
        [weakSelf.wallets addObjectsFromArray:items];
        [weakSelf.collectionView reloadData];
    }];
    
}
- (void)pais_change{
    [self.collectionView.mj_header beginRefreshing];
//    [self qureyAccountInfo];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section) {
        return self.wallets.count;
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section) {
        ZWalletListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZWalletListCell" forIndexPath:indexPath];
        ZWalletAccountInfo *accountInfo = self.wallets[indexPath.item];
        cell.nameLabel.text = accountInfo.name;
        cell.subTitleLabel.text = accountInfo.walletAdress;
        cell.tagLabel.text  = [ZWalletAccount wallet].currentCurrrencyUnit;
        [self requestCoinNumberData:accountInfo cell:cell];
        return cell;
    }
    ZMyHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZMyHeaderCell" forIndexPath:indexPath];
    cell.backgroundColor = RGBCOLOR(62, 185, 199);
    NSArray *titles = @[@"createWallet",@"importWallet"];
    NSArray *images = @[@"create_wallet",@"import_wallet"];
    NSDictionary *dic = @{
                          @"labelTexts":titles,
                          @"images":images
                          };
    __weak typeof(UINavigationController *) weakNav = self.navigationController;
    cell.clickBtn = ^(UIButton *button) {
        if (button.tag == 0) {
            ZCreateWalletController *vc = [ZCreateWalletController new];
            [weakNav pushViewController:vc animated:YES];
        }else
        {
            ZImportPrivateController *import = [ZImportPrivateController new];
            [weakNav pushViewController:import animated:YES];
        }
    };
    cell.item = dic;
    return cell;
}

-(void)requestCoinNumberData:(ZWalletAccountInfo *)accountInfo cell:(ZWalletListCell *)cell{
    
//    ZWWeakSelf;
    NSString *url = [NSString stringWithFormat:@"%@wallet/getAssetList",PortServer];
    NSDictionary *param  = @{@"address":accountInfo.walletAdress};
    ZWLog(@"url = %@\n param = %@",url,param);
    cell.numberLabel.text = @"";
    [ZNetManager GET:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
        NSInteger status = [responseObject[@"status"] integerValue];
        if(status == 1){
            ZWalletInfo *info = [ZWalletInfo mj_objectWithKeyValues:responseObject[@"data"]];
            cell.numberLabel.text  = [NSString stringWithFormat:@"%.2lf",[info.total doubleValue]];
        }else
        {
            [ZProgressManager showError:responseObject[@"message"]];
        }
        ZWLog(@"responseObject = %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        [ZProgressManager showError:@"连接失败"];
        ZWLog(@"error = %@",error.userInfo);
    }];
}
#pragma mark -- UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return;
    }
    
    ZWalletDetailController *detailVC = [ZWalletDetailController new];
    detailVC.account = self.wallets[indexPath.item];
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark -- UICollectionViewDelegateFlowLayout


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat width = SCREEN_W;
    CGFloat height = 180;
    if (indexPath.section) {
        return CGSizeMake(width- 20, 50 + 70);
    }
    return CGSizeMake(width, height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section) {
        return UIEdgeInsetsMake(10, 10, 0, 10);
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (section) {
        return 10;
    }
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;



@end
